package client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.SoftBevelBorder;

public class PanelCheckBorder extends JPanel {
	private static final long serialVersionUID = 2426044419762183615L;

	private JCheckBox checkBox = new JCheckBox();
	private ComponentTitledBorder border = null;
	private Border borderDefault = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

	private Border borderCheckOnFocusGain = BorderFactory.createSoftBevelBorder(SoftBevelBorder.LOWERED);
	private Border borderCheckOnFocusLost = BorderFactory.createSoftBevelBorder(SoftBevelBorder.RAISED);

	public PanelCheckBorder(String text) {
		this();
		checkBox.setText(text);
	}

	public void setText(String text) {
		checkBox.setText(text);
	}

	public PanelCheckBorder() {
		super(new BorderLayout());
		border = new ComponentTitledBorder(checkBox, this, borderDefault);
		this.setBorder(border);

		checkBox.setFont(checkBox.getFont().deriveFont(Font.PLAIN));

		checkBox.setBorder(borderCheckOnFocusLost);

		checkBox.setOpaque(true);
		checkBox.setBorderPainted(true);

		checkBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setSelectedAll(PanelCheckBorder.this, checkBox.isSelected());
			}
		});

		this.setFocusable(true);
		this.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				checkBox.setBorder(borderCheckOnFocusGain);
				repaint();
			}

			@Override
			public void focusLost(FocusEvent e) {
				checkBox.setBorder(borderCheckOnFocusLost);
				repaint();
			}
		});

		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
					checkBox.doClick();
					repaint();
				}
			}
		});
	}

	public JCheckBox getCheckBox() {
		return checkBox;
	}

	private void setSelectedAll(Container container, boolean isSelected) {
		Component[] c = container.getComponents();
		for (Component component : c) {
			if (component instanceof JCheckBox) {
				((JCheckBox) component).setSelected(isSelected);
			} else {
				if (component instanceof Container) {
					setSelectedAll((Container) component, isSelected);
				}
			}
		}
	}

	public static void main(String[] args) {

		JPanel panel = new JPanel(new GridLayout(3, 3));

		for (int i = 0; i < 9; i++) {
			JCheckBox jCheckBox = new JCheckBox("" + i);

			panel.add(jCheckBox);
		}

		PanelCheckBorder panelCheckBorder = new PanelCheckBorder("Todos/Nenhum");

		panelCheckBorder.add(panel);

		JOptionPane.showMessageDialog(null, panelCheckBorder);
	}
}
